<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html   xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>后台管理 - 易买网</title>
<link type="text/css" rel="stylesheet" href="../css/style.css" />
<script type="text/javascript" src="../scripts/function-manage.js"></script>
	<script type="text/javascript" src="../scripts/jquery-1.8.3.js"></script>
</head>
<body>

	<div class="main">
		<h2>添加新闻</h2>
		<a href="javascript:history.go(-1)"><h2><<返回上一级</h2></a>
		<div class="manage">
			<form action="../AddNewsServlet" enctype="multipart/form-data" method="post">
				<table class="form">
					<tr>
						<td class="field">新闻标题：</td>
						<td><input type="text" class="text" name="title" value=""  required/></td>
					</tr>
					<tr>
						<td class="field">新闻内容：</td>
						<td><textarea name="content" style="resize: none"></textarea></td>
					</tr>
					<tr>
						<td class="field">上传图片：</td>
						<td><input type="file" name="pic">  &nbsp;分类:
							<select name="parentClass" id="parentClass" onchange="parentChange()">
								<option value="0" selected="selected">请先选择</option>
								<c:forEach items="${list}" var="v">
									<option value="${v.epcid}">${v.epcname}</option>
								</c:forEach>
							</select>
							&nbsp;&nbsp;子类:
							<select name="ChildClass" id="ChildClass" onchange="productshow()" required>


							</select>

						</td>
					</tr>
					<tr>
						<td class="field">选择板块：</td>
                        <td><input type="radio" name="enflag" value="0" id="cuxiao" checked ><label for="cuxiao">促销信息</label> &nbsp;<input type="radio" name="enflag" value="1" id="gonggao"> <label for="gonggao">新闻公告</label>
                              &nbsp;&nbsp;
							<a href="javascript:void(0)" onclick="productshow()">选择关联商品(无需添加请点击取消关联)</a>
							&nbsp;&nbsp;
							<input type="text" value="" readonly id="referencename" size="20" >
							&nbsp;&nbsp;
							<a href="javascript:void(0)" onclick="canclerel()">取消关联</a>
							<input type="text" value="0" hidden id="reference" name="epid"/>
						</td>
					</tr>
					<tr>
						<td></td>
						<td><label class="ui-blue"><input type="submit" name="submit" value="添加" /></label></td>
					</tr>
				</table>
			</form>
		</div>
	</div>
	<div class="clear"></div>
</div>
<div id="footer">
	Copyright &copy; 2018 云和数据 All Rights Reserved. 京ICP证1000001号
</div>
<div>
	<div id="cglist" style="display: inline"></div>
	<div id="detail_list" style="float: right ;position: absolute ;top: 10px ;right: 200px" >
		<table class="list" id="tbproduct">
			<tr>
				<td colspan="2" width="50%" class="bankuai" id="asd" style="background-color: #fc7e31">商品列表</td>
			<tr>
				<th>商品名称</th>
				<th>添加关联</th>
			</tr>
		</table>
		<a href="javascript:void (0)" id="firstpage" class="pages" fenye='CuXiaoFenYe'>首页</a>
		<a href="javascript:void (0)" id="prevpage" class="pages"  fenye='CuXiaoFenYe'>上一页</a>
		<a href="javascript:void (0)" id="nextpage" class="pages" fenye='CuXiaoFenYe'>下一页</a>
		<a href="javascript:void (0)" id="lastpage" class="pages" fenye='CuXiaoFenYe'>尾页</a>

	</div>

</div>
</body>
</html>
<script type="text/javascript" >

    var pageIndex;//当前页
    var totalPage;//总页数

	$(function () {

    })

    //异步分页获取商品信息
    var getProductlist = function(curr){
        $.ajax({
            type:'POST',
            url:'../GetProductByChildrenJsonServlet',
            dataType:'JSON',
            data: {
                epcid: $("#ChildClass").val(),
                pageIndex: curr || 1
            },
            success:function(data){
                $("#tbproduct").find(".contextdata").remove();
                $("#tbproduct").find(".page").remove();
                $.each(data.productList,function (index,obj) {
                    var $newsStr="<tr class='contextdata'><td>"+ obj.epname+"</td><td><a href='javascript:void(0)' onclick='producthide("+obj.epid+',"'+obj.epname+'"'+")'>添加关联</a></td></tr>";
                    $("#tbproduct").append($newsStr);
                })
                var $pageinfor="<tr class='page'><td colspan=4>共有<span id='total_page'>"+data.totalPages+"<span>页&nbsp;"+data.totalRows+"条记录&nbsp;当前为第<span id='curr_page'>"+data.pageIndex+"</span>页</td></tr>"
                $("#tbproduct").append($pageinfor);

                totalPage = data.totalPages;
                pageIndex = data.pageIndex;
            }
        })
    }

    function producthide(id,epname){
        $("#detail_list").css("display","none");
        $("#referencename").val(epname);
        $("#reference").val(id);
	}

	function productshow(){
        $("#detail_list").css("display","block");
        getProductlist();
	}

	//异步控制子分类下拉框信息
	function parentChange(){
        var pid = $("#parentClass").val()
        $.ajax({
			type:'POST',
			url:'../ToNewsAddChildrenListServlet',
			dataType:'JSON',
			data:{
			    pid:pid
			},
			success:function (data) {
                $("#ChildClass").find(".childop").remove();
				$.each(data,function (index,obj) {
					var $str = "<option name ='childId' class='childop' value="+obj.epcid+">"+obj.epcname+"</option>"
                    $("#ChildClass").append($str);
                })
            }
		})
	}

    //促销信息分页
    function cuxiaofenye(){
        $("[fenye='CuXiaoFenYe']").unbind()
        $("[fenye='CuXiaoFenYe']").on("click",function () {
            var pageflag = $(this).attr("id")

            if (pageflag=="firstpage"){
                getCuXiaoNews(1);
            } else if (pageflag=="prevpage"){
                pageIndex--;
                if(pageIndex<1){
                    pageIndex=1;
                }
                getCuXiaoNews(pageIndex);
            } else if (pageflag == "nextpage"){
                pageIndex++;
                if(pageIndex>totalPage){
                    pageIndex=totalPage;
                }
                getCuXiaoNews(pageIndex);
            } else if(pageflag=="lastpage"){
                getCuXiaoNews(totalPage);
            }
        })
    }

    //取消关联商品
    function canclerel(){
        $("#referencename").val("");
        $("#reference").val(0);
    }

</script>

